package com.yunze.apiCommon.upstreamAPI.SDJinDouYun.Inquire;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunze.apiCommon.upstreamAPI.SDJinDouYun.JinDouYun_Api;
import com.yunze.apiCommon.utils.HttpUtil;
import com.yunze.apiCommon.utils.MD5Util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 查询类接口
 */
public class Query_JinDouYun extends JinDouYun_Api {
    protected Logger logger = Logger.getLogger(String.valueOf(this.getClass()));

    public Query_JinDouYun(Map<String, Object> init_map) {
        super(init_map);
    }

    public Map<String,Object>  SelCard(Map<String, Object> map){ //查询 用量 余额 激活时间 状态:state  expired_at:到期日期
        String id = "";//参数
        //String address = "/api/v1/getChaxun?";// 请求地址

        String url = "http://api.m2m-10086.cn:32003/api/v1/getChaxun?"; // 拼接之后的 请求地址:http://api.m2m-10086.cn:32003/api/v1/getChaxun?userId=10000&cardno=1064925994085&times=1484139646&sign=9E1A28E0377DA2AFF3E55707657485EF
        long times = System.currentTimeMillis() / 1000;
        String iccid = map.get("iccid").toString(); // 卡号
        id= user_id;
        String psa= key;
        String sign = MD5Util.MD5Encode("userId="+id+"&apikey="+psa+"&times="+times).toUpperCase(); // 获取sign
        String pinUrl= url+"userId="+id+"&cardno="+iccid+"&times="+times+"&sign="+sign;//最终请求路径

        String res = HttpUtil.get(pinUrl);//结果集

        Map<String, Object> rMap = new HashMap<>(); //return 对象
        Double used = 0.0;//用量
        String active = null;//激活时间
        String state = null;//状态
        Double surplus = 0.0;//剩余用量
        String expired_at = null;//到期时间
        String code = "500";
        String Message = "";
        JSONObject data_map = JSON.parseObject(res);
        if (data_map.get("code").equals(0)){ //请求成功
            Map<String, Object> data = (Map<String, Object>) data_map.get("data");

             used=Double.parseDouble(data.get(getUsedMonth()).toString());
             active=data.get("active_date").toString();
             state = data.get("state").toString();
             surplus = Double.parseDouble(data.get("surplus").toString());
             expired_at = data.get("expired_at").toString();
             code = "200";

        }
        rMap.put("used",used);
        rMap.put("active",active);
        rMap.put("state",state);
        rMap.put("surplus",surplus);
        rMap.put("expired_at",expired_at);
        rMap.put("code",code);
        rMap.put("Message",Message);

        return rMap;
    }


    public Map<String,Object>  changeCardStats(Map<String, Object> map){
        //变更卡状态
        // 复机:http://%7B%7Bhost%7D%7D/api/v1/start?userId=10000&cardno=1064925994085&times=1484139646&sign=9E1A28E0377DA2AFF3E55707657485EF
        // 停机:http://%7B%7Bhost%7D%7D/api/v1/stop?userId=10000&cardno=1064925994085&times=1484139646&sign=9E1A28E0377DA2AFF3E55707657485EF
        //区分 状态 做停复机 操作

        String address =null;
        if (map.get("Is_Stop").equals("off")){
            //停机
            address = "/api/v1/stop?";
        }else if (map.get("Is_Stop").equals("on")){
            //复机
            address="/api/v1/start?";
        }
        String url = server_Ip + address; // 拼接之后的 请求地址
        long times = System.currentTimeMillis() / 1000; //10位制时间戳
        String sign = getSign(times); // 获取sign
        String iccid = null;
        iccid = map.get("iccid").toString();
        String pinUrl= url+"userId="+user_id+"&cardno="+iccid+"&times="+times+"&sign="+sign;//最终请求路径
        String res = HttpUtil.get(pinUrl);//结果集
        //System.out.println(res);
        JSONObject jsonObject = JSON.parseObject(res); // 成功结果:{"msg":"操作成功","code":0,"data":[]}


        return  jsonObject;
    }


    public static void main(String[] args) {
        //89860444032090070001
        //String address ="/api/v1/start?";
        //String address ="/api/v1/stop?";
        //:http://域名/api/v1/getChaxun
        user_id = "2339";
        key="8f7abefd17e6345ce25a445e03687d8f ";
        String address ="/api/v1/getChaxun?";
        String url = server_Ip + address; // 拼接之后的 请求地址
        String iccid = "898604B61522C0030000";
        long times = System.currentTimeMillis() / 1000; //10位制时间戳
        String sign = getSign(times); // 获取sign
        String pinUrl = url+"userId="+user_id+"&cardno="+iccid+"&times="+times+"&sign="+sign;
        System.out.println(pinUrl);
        String res = HttpUtil.get(pinUrl);//结果集
        //System.out.println(res);
        JSONObject jsonObject = JSON.parseObject(res);
        System.out.println(jsonObject);
    }

    public String getUsedMonth(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM");
        Date date = new Date(System.currentTimeMillis());
        String used_date = formatter.format(date);
        String[] split = used_date.split("-");
        String used_month = "used"+split[0]+split[1];
        return used_month;
    }

}
